FUNCTION ZFIFM_VIM_PREPROCESS_EXIT.
*"----------------------------------------------------------------------
*"*"本地接口：
*"  CHANGING
*"     REFERENCE(INDEXER) TYPE REF TO  /OPT/CL_VIM_DOC_PROCESSOR
*"----------------------------------------------------------------------
  DATA:
    LV_MWSKZ        TYPE EKPO-MWSKZ,
    LV_MWSKZ_DIFF   TYPE C,
    LV_ZWELS        TYPE LFB1-ZWELS,
    LS_ZFIT079_HEAD TYPE ZFIT079_HEAD,
    LT_EBELN        TYPE TABLE OF EKKO-EBELN WITH HEADER LINE,
    LT_PO_DN        TYPE TABLE OF /OPT/VIM_1PO_DN WITH HEADER LINE,
    LT_ZFIT079_ITEM TYPE TABLE OF ZFIT079_ITEM WITH HEADER LINE,
    LS_DP_ITEM      LIKE LINE OF INDEXER->INDEX_ITEM,
    LV_JSDDH        TYPE ZFIT079_HEAD-JSDDH,
    LS_ZFIT078_HIS  TYPE ZFIT078_HIS,
    LT_ZFIT078_HIS  TYPE STANDARD TABLE OF ZFIT078_HIS.

  IF INDEXER->INDEX_HEADER-DOCTYPE = GC_DP_ZNONPO01
    OR INDEXER->INDEX_HEADER-DOCTYPE = GC_DP_ZPO01.

    LV_JSDDH = INDEXER->INDEX_HEADER-CUSTOM_FIELD0.

    SELECT SINGLE *
     INTO LS_ZFIT079_HEAD
     FROM ZFIT079_HEAD
    WHERE JSDDH = LV_JSDDH.

    SELECT *
      INTO TABLE LT_ZFIT079_ITEM
      FROM ZFIT079_ITEM
     WHERE JSDDH = LV_JSDDH.


    "设抬头置默认值
    IF INDEXER->INDEX_HEADER-POST_DATE_DET IS INITIAL.
      INDEXER->INDEX_HEADER-POST_DATE_DET = 'C'.  "Posting Date Detemination
      INDEXER->INDEX_HEADER-BUDAT = SY-DATUM.
    ENDIF.
    INDEXER->INDEX_HEADER-ZUONR = INDEXER->INDEX_HEADER-DOCID.
    INDEXER->INDEX_HEADER-SGTXT = LS_ZFIT079_HEAD-SGTXT.

    IF INDEXER->INDEX_HEADER-PAYMENT_METHOD IS INITIAL.
      SELECT SINGLE
        ZWELS
        INTO LV_ZWELS
        FROM LFB1
       WHERE LIFNR = INDEXER->INDEX_HEADER-LIFNR
         AND BUKRS = INDEXER->INDEX_HEADER-BUKRS.

      CONDENSE LV_ZWELS.

      IF STRLEN( LV_ZWELS ) = 1."供应商只有一种付款方式的话，直接带过来
        INDEXER->INDEX_HEADER-PAYMENT_METHOD =   LV_ZWELS.
      ENDIF.
    ENDIF.

    IF INDEXER->INDEX_HEADER-DOCTYPE = GC_DP_ZNONPO01.
      INDEXER->INDEX_HEADER-BLART = 'KR'.
    ELSE.
      INDEXER->INDEX_HEADER-BLART = 'RE'.
    ENDIF.

    IF INDEXER->INDEX_HEADER-VORGANG = '2'. "贷项凭证
      INDEXER->INDEX_HEADER-BLART = 'KG'.
    ENDIF.

    "设置DP行项目
    LOOP AT LT_ZFIT079_ITEM.
      CLEAR LS_DP_ITEM.
      LS_DP_ITEM-DOCID = INDEXER->INDEX_HEADER-DOCID.
      LS_DP_ITEM-ITEMID = SY-TABIX.
      LS_DP_ITEM-ZUONR = INDEXER->INDEX_HEADER-DOCID.


      IF INDEXER->INDEX_HEADER-DOCTYPE = GC_DP_ZNONPO01.
        LS_DP_ITEM-HKONT     = LT_ZFIT079_ITEM-GL_HKONT.
        LS_DP_ITEM-SHKZG     = LT_ZFIT079_ITEM-GL_SHKZG.
        LS_DP_ITEM-WRBTR     = LT_ZFIT079_ITEM-GL_WRBTR.
        LS_DP_ITEM-TAX_CODE1 = LT_ZFIT079_ITEM-GL_MWSKZ.
        LS_DP_ITEM-KOSTL     = LT_ZFIT079_ITEM-GL_KOSTL.
        LS_DP_ITEM-BUKRS     = LS_ZFIT079_HEAD-BUKRS.

        IF LT_ZFIT079_ITEM-GL_SGTXT IS NOT INITIAL.
          LS_DP_ITEM-SGTXT     = LT_ZFIT079_ITEM-GL_SGTXT.
        ELSE.
          LS_DP_ITEM-SGTXT = INDEXER->INDEX_HEADER-SGTXT.  "行项目文本 放结算单项目文本
        ENDIF.

        IF LV_MWSKZ IS INITIAL.
          LV_MWSKZ = LT_ZFIT079_ITEM-GL_MWSKZ.
        ELSE.
          IF LV_MWSKZ <> LT_ZFIT079_ITEM-GL_MWSKZ.
            LV_MWSKZ_DIFF = 'X'.
          ENDIF.
        ENDIF.

      ELSE. "基于PO的发票
        IF LT_ZFIT079_ITEM-EBELN IS INITIAL."有可能存在发票行数多于PO收货行，该行只存发票号码
          CONTINUE.
        ENDIF.

        LS_DP_ITEM-EBELN = LT_ZFIT079_ITEM-EBELN.
        LS_DP_ITEM-EBELP = LT_ZFIT079_ITEM-EBELP.
        LS_DP_ITEM-MATNR = LT_ZFIT079_ITEM-MATNR.
        LS_DP_ITEM-SHKZG = LT_ZFIT079_ITEM-SHKZG.
        LS_DP_ITEM-MAKTX = LT_ZFIT079_ITEM-TXZ01.
        LS_DP_ITEM-WRBTR = LT_ZFIT079_ITEM-WRBTR + LT_ZFIT079_ITEM-BEZNK .
        LS_DP_ITEM-MENGE = LT_ZFIT079_ITEM-ERFMG.
        LS_DP_ITEM-BSTME = LT_ZFIT079_ITEM-ERFME.
        LS_DP_ITEM-TAX_CODE1 = LT_ZFIT079_ITEM-MWSKZ.
        LS_DP_ITEM-BUKRS = LS_ZFIT079_HEAD-BUKRS.
        LS_DP_ITEM-WERKS = LT_ZFIT079_ITEM-WERKS.
        LS_DP_ITEM-REF_DOC = LT_ZFIT079_ITEM-MBLNR.
        LS_DP_ITEM-REF_DOC_YEAR = LT_ZFIT079_ITEM-MJAHR.
        LS_DP_ITEM-REF_DOC_IT = LT_ZFIT079_ITEM-ZEILE.
        LS_DP_ITEM-SHEET_NO = LT_ZFIT079_ITEM-SHEET_NO.
        LS_DP_ITEM-COND_TYPE = LT_ZFIT079_ITEM-COND_TYPE.
        LS_DP_ITEM-LFSNR = LT_ZFIT079_ITEM-LFSNR.

        LS_DP_ITEM-SGTXT = INDEXER->INDEX_HEADER-SGTXT.  "行项目文本 放结算单项目文本

        IF LV_MWSKZ IS INITIAL.
          LV_MWSKZ = LT_ZFIT079_ITEM-MWSKZ.
        ELSE.
          IF LV_MWSKZ <> LT_ZFIT079_ITEM-MWSKZ.
            LV_MWSKZ_DIFF = 'X'.
          ENDIF.
        ENDIF.

        IF LS_DP_ITEM-EBELN IS NOT INITIAL.
          CLEAR LT_EBELN.
          LT_EBELN = LS_DP_ITEM-EBELN.
          APPEND LT_EBELN.
        ENDIF.

      ENDIF.


      APPEND LS_DP_ITEM TO INDEXER->INDEX_ITEM.
    ENDLOOP.

    "抬头税码填充
    IF LV_MWSKZ IS NOT INITIAL AND LV_MWSKZ_DIFF IS INITIAL.
      INDEXER->INDEX_HEADER-TAX_CODE = LV_MWSKZ.
    ENDIF.

    "PO List 填充
    IF LT_EBELN[] IS NOT INITIAL .
      SORT LT_EBELN.
      DELETE ADJACENT DUPLICATES FROM LT_EBELN COMPARING ALL FIELDS.

      IF LINES( LT_EBELN[] ) = 1.
        READ TABLE LT_EBELN INDEX 1.
        INDEXER->INDEX_HEADER-EBELN = LT_EBELN.

      ELSE.
        LOOP AT LT_EBELN.
          IF INDEXER->INDEX_HEADER-EBELN IS INITIAL.
            INDEXER->INDEX_HEADER-EBELN = LT_EBELN.
          ENDIF.

          CLEAR LT_PO_DN.
          LT_PO_DN-DOCID = INDEXER->INDEX_HEADER-DOCID.
          LT_PO_DN-EBELN = LT_EBELN.
          LT_PO_DN-COUNTER = SY-TABIX.
          APPEND LT_PO_DN.
        ENDLOOP.

        INDEXER->INDEX_LIST = LT_PO_DN[].
      ENDIF.
    ENDIF.


    "进VIM后设置发票结算单状态为03：处理中
    IF LS_ZFIT079_HEAD IS NOT INITIAL.
      LS_ZFIT079_HEAD-JSDZT = '03'."处理中
      LS_ZFIT079_HEAD-DOCID = INDEXER->INDEX_HEADER-DOCID.
      LS_ZFIT079_HEAD-CHNAM = SY-UNAME.
      LS_ZFIT079_HEAD-CHDAT = SY-DATUM.
      LS_ZFIT079_HEAD-CHTIM = SY-UZEIT.

      "保存发票处理历史
      IF LT_ZFIT079_ITEM[] IS NOT INITIAL.
        LOOP AT LT_ZFIT079_ITEM.
          CLEAR LS_ZFIT078_HIS.
          LS_ZFIT078_HIS-ZFPHM = LT_ZFIT079_ITEM-ZYSFPHM.
          LS_ZFIT078_HIS-CHNAM = SY-UNAME.
          LS_ZFIT078_HIS-CHDAT = SY-DATUM.
          LS_ZFIT078_HIS-CHTIM = SY-UZEIT.
          LS_ZFIT078_HIS-ZFPZT = '30'.  "DP 处理中
          LS_ZFIT078_HIS-JSDDH = LT_ZFIT079_ITEM-JSDDH.
          LS_ZFIT078_HIS-EDI_DOCNUM = LS_ZFIT079_HEAD-EDI_DOCNUM.
          LS_ZFIT078_HIS-DOCID = LS_ZFIT079_HEAD-DOCID.
          LS_ZFIT078_HIS-BELNR = LS_ZFIT079_HEAD-BELNR.
          LS_ZFIT078_HIS-GJAHR = LS_ZFIT079_HEAD-GJAHR.
          APPEND LS_ZFIT078_HIS TO LT_ZFIT078_HIS.
        ENDLOOP.

        SORT LT_ZFIT078_HIS.
        DELETE ADJACENT DUPLICATES FROM LT_ZFIT078_HIS COMPARING ALL FIELDS.
      ENDIF.

      MODIFY ZFIT079_HEAD FROM LS_ZFIT079_HEAD.
      IF LT_ZFIT078_HIS[] IS NOT INITIAL.
        MODIFY ZFIT078_HIS FROM TABLE LT_ZFIT078_HIS.
      ENDIF.
    ENDIF.

  ENDIF.
ENDFUNCTION.
